package com.xinghou.mq.impl;

import com.spatial4j.core.context.SpatialContext;
import com.xinghou.mq.UserMqInterface;
import com.xinghou.platform.dao.main.CityDAO;
import com.xinghou.platform.dao.main.StoreDAO;
import com.xinghou.platform.dao.main.UserDAO;
import com.xinghou.platform.dao.main.UserGeoDAO;
import com.xinghou.platform.lucene.main.UserLuceneService;
import com.xinghou.platform.model.main.User;
import com.xinghou.platform.util.ConfigUtil;
import org.apache.lucene.spatial.SpatialStrategy;
import org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy;
import org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree;
import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Closeable;
import java.io.IOException;

@Service("mqUserMqService")
public class UserMqInterfaceImpl implements UserMqInterface, Closeable {
	@Resource
	private UserDAO userDAO;
	@Resource
	private UserGeoDAO userGeoDAO;
	@Resource
	private CityDAO cityDAO;
	@Resource
	private StoreDAO storeDAO;
	
	@Resource
	private UserLuceneService userLuceneService;

	public UserMqInterfaceImpl() {

	}

	public static final double RATE_MILE_TO_KM = 1.609344; // 英里和公里的比率
	public static final String LAT_FIELD = "lat";
	public static final String LON_FIELD = "lng";

	private static String INDEX_DIR = ConfigUtil.getProperty("luceneIndex");
	private SpatialContext ctx = SpatialContext.GEO;// "ctx" is the conventional
													// variable name
	int maxLevels = 11;
	SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
	private SpatialStrategy strategy = new RecursivePrefixTreeStrategy(grid, "userGeoField");

	@Override
	public void update(int userId,boolean update) {
		User user = userDAO.getUserById(userId);
        if(update){
        	userLuceneService.updateUserInfo(user);
        }else{
        	userLuceneService.saveUserInfo(user);
        }
	}

 

	@Override
	public void close() throws IOException {

	}

}
